﻿using System.Text.RegularExpressions;

namespace WikiUploader.Classes.DotNetWikiBot
{
   public static class RegExes
   {
      private static readonly Regex s_LinkToPage = new Regex("<li><a href=\"[^\"]*?\" (?:class=\"mw-redirect\" )?title=\"([^\"]+?)\">");
      private static readonly Regex s_LinkToPage2 = new Regex("<a href=\"[^\"]*?\" title=\"([^\"]+?)\">\\1</a>");
      private static readonly Regex s_LinkToSubCategory = new Regex(">([^<]+)</a></div>\\s*<div class=\"CategoryTreeChildren\"");
      private static readonly Regex s_LinkToImage = new Regex ("<div class=\"gallerytext\">\n<a href=\"[^\"]*?\" title=\"([^\"]+?)\">");
      private static readonly Regex s_PageTitleTag = new Regex("<title>(.+?)</title>");
      private static readonly Regex s_WikiLink = new Regex(@"\[\[(.+?)(\|.+?)?]]");

      /// <summary>Regular expression to find wiki templates in markup.</summary>
      public static Regex WikiTemplate = new Regex(@"(?s)\{\{(.+?)((\|.*?)*?)}}");

      /// <summary>Regular expression to find links to sister wiki projects in markup.</summary>
      public static Regex SisterWikiLink;

      /// <summary>Regular expression to find interwiki links in wiki markup.</summary>
      public static Regex InterWikiLink;

      /// <summary>Regular expression to find displayed interwiki links in wiki markup,
      /// like "[[:de:...]]".</summary>
      public static Regex InterWikiDisplayedLink;

      /// <summary>Regular expression to find external web links in wiki markup.</summary>
      public static Regex WebLink =
         new Regex("(https?|t?ftp|news|nntp|telnet|irc|gopher)://([^\\s'\"<>]+)");

      /// <summary>Regular expression to find sections of text, that are explicitly
      /// marked as non-wiki with special tag.</summary>
      public static Regex NoWikiMarkup = new Regex("(?is)<nowiki>(.*?)</nowiki>");

      /// <summary>Regular expression to extract language code from site URL.</summary>
      public static Regex SiteLanguage = new Regex(@"https?://(.*?)\.(.+?\..+)");

      /// <summary>Regular expression to extract edit session time attribute.</summary>
      public static Regex EditSessionTime =
         new Regex("value=\"([^\"]*?)\" name=['\"]wpEdittime['\"]");

      /// <summary>Regular expression to extract edit session time attribute.</summary>
      public static Regex EditSessionTime2 = new Regex(" touched=\"(.+?)\"");

      /// <summary>Regular expression to extract edit session token attribute.</summary>
      public static Regex EditSessionToken =
         new Regex("value=\"([^\"]*?)\" name=['\"]wpEditToken['\"]");

      /// <summary>Regular expression to extract edit session token attribute.</summary>
      public static Regex EditSessionToken2 =
         new Regex("name=['\"]wpEditToken['\"](?: type=\"hidden\")? value=\"([^\"]*?)\"");

      /// <summary>Regular expression to extract edit session token attribute.</summary>
      public static Regex EditSessionToken3 = new Regex(" edittoken=\"(.+?)\"");

      /// <summary>
      /// Gets the regular expression to find links to pages in list in HTML source.
      /// </summary>
      public static Regex LinkToPage
      {
         get { return s_LinkToPage; }
      }

      /// <summary>
      /// Gets an alternative regular expression to find links to pages in HTML source.
      /// </summary>
      public static Regex LinkToPage2
      {
         get { return s_LinkToPage2; }
      }

      /// <summary>
      /// Gets the regular expression to find links to subcategories in HTML source
      /// of category page on sites that use "CategoryTree" MediaWiki extension.
      /// </summary>
      public static Regex LinkToSubCategory
      {
         get { return s_LinkToSubCategory; }
      }

      /// <summary>
      /// Gets the regular expression to find links to image pages in galleries
      /// in HTML source.
      /// </summary>
      public static Regex LinkToImage
      {
         get { return s_LinkToImage; }
      }

      /// <summary>
      /// Gets the regular expression to find titles in markup.
      /// </summary>
      public static Regex PageTitleTag
      {
         get { return s_PageTitleTag; }
      }

      /// <summary>
      /// Gets the regular expression to find internal wiki links in markup.
      /// </summary>
      public static Regex WikiLink
      {
         get { return s_WikiLink; }
      }
   }
}
